package vincent.com.layouttest.anim;

import android.animation.Animator;
import android.animation.ObjectAnimator;
import android.annotation.TargetApi;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.LinearInterpolator;
import android.widget.ImageView;

import vincent.com.layouttest.ColorStatusActivity;
import vincent.com.layouttest.R;
import vincent.com.layouttest.util.ImageUtil;
import vincent.com.layouttest.util.LogUtil;

/**
 * Created by zhengwenchen on 2016/4/14.
 */
public class RotateAimSampleActivity extends ColorStatusActivity implements Animator.AnimatorListener{

    private ImageView mAlbumImg;
    private ObjectAnimator mRotateAnimtor;

    private boolean mIsAniming = false;

    private float mLastRotation = 0;

    private final long wholeDuration = 20000;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mAlbumImg = (ImageView)findViewById(R.id.album_img);

        mRotateAnimtor = ObjectAnimator.ofFloat(mAlbumImg, "rotation", 0f, 360f);
        mRotateAnimtor.setRepeatCount(ObjectAnimator.INFINITE);
        mRotateAnimtor.setInterpolator(new LinearInterpolator());
        mRotateAnimtor.addListener(this);
        mRotateAnimtor.setDuration(wholeDuration);
        mRotateAnimtor.end();

        Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.my_myalbum_img);
        Bitmap circle = ImageUtil.circleHead(bitmap, bitmap.getWidth(), bitmap.getHeight(), 0, Color.TRANSPARENT);

        mAlbumImg.setImageBitmap(circle);

        try {
            bitmap.recycle();
            bitmap = null;
        }catch (Exception e){
            Log.e("czw_exception", e.getMessage());
        }
    }

    @Override
    public View onCreateView(ViewGroup rootView) {
        return LayoutInflater.from(this).inflate(R.layout.activity_rotate_anim_sample, rootView, true);
    }

    public void startAnim(View v){
        LogUtil.i("czw_rotate_anim", "startAnim");

        if(mLastRotation > 0){
            mRotateAnimtor.setFloatValues(mLastRotation, 360f);
            mRotateAnimtor.setDuration(computeTime(Math.abs(360f - mLastRotation)));
            mRotateAnimtor.setRepeatCount(0);
        }

        mRotateAnimtor.start();
        mIsAniming = true;
    }

    @Override
    public String getToolbarTitle() {
        return "Rotate Animation";
    }

    @Override
    public void onAnimationStart(Animator animation) {
        LogUtil.i("czw_rotate_anim", "onAnimationStart rotation: " + mAlbumImg.getRotation());
    }

    @Override
    public void onAnimationEnd(Animator animation) {

        LogUtil.i("czw_rotate_anim", "onAnimationEnd");

        if(mRotateAnimtor.getRepeatCount() != ObjectAnimator.INFINITE){
            mRotateAnimtor.setRepeatCount(ObjectAnimator.INFINITE);
            mRotateAnimtor.setFloatValues(0f, 360f);
            mRotateAnimtor.setDuration(wholeDuration);
            mRotateAnimtor.start();
        }
    }

    @Override
    public void onAnimationCancel(Animator animation) {
        mLastRotation = mAlbumImg.getRotation();
        LogUtil.i("czw_rotate_anim", "onAnimationCancel rotation: " + mLastRotation);
    }

    @Override
    public void onAnimationRepeat(Animator animation) {
        LogUtil.i("czw_rotate_anim", "onAnimationRepeat");
    }

    @Override
    protected void onPause() {
        super.onPause();
        if(mIsAniming){
            mRotateAnimtor.cancel();
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        if(mIsAniming){
            mRotateAnimtor.start();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mRotateAnimtor.cancel();
    }

    public void cancelAnim(View target){
        LogUtil.i("czw_rotate_anim", "cancelAnim");
        mRotateAnimtor.cancel();
        mIsAniming = false;
    }

    public void endAnim(View target){
        LogUtil.i("czw_rotate_anim", "endAnim");
        mRotateAnimtor.end();
    }

    private long computeTime(float angleDuration){
        return (long)(angleDuration/360f * wholeDuration);
    }
}
